package com.crm.excel.upload;

import com.crm.pojo.SaleChance;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SaleChanceExcel extends BaseExcel<SaleChance> {
    @SuppressWarnings("deprecation")
    @Override
    public List<SaleChance> excelValue(Workbook wb) throws ParseException {
        // 得到第一个shell
        Sheet sheet = wb.getSheetAt(0);
        // 得到Excel的行数
        this.totalRows = sheet.getPhysicalNumberOfRows();
        // 得到Excel的列数(前提是有行数)
        if (totalRows > 1 && sheet.getRow(0) != null) {
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }
        List<SaleChance> saleChanceList = new ArrayList<>();
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");

        // 循环Excel行数
        for (int r = 1; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            SaleChance saleChance = new SaleChance();
            for (int c = 0; c < this.totalCells; c++) {
                Cell cell = row.getCell(c);
                if(null != cell){
                    if(c == 0){
                        // 如果是纯数字,比如你写的是25,cell.getNumericCellValue()获得是25.0,通过截取字符串去掉.0获得25
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String chidStr = String.valueOf(cell.getNumericCellValue());
                            chidStr = chidStr.substring(0, chidStr.length() - 2 > 0 ? chidStr.length() - 2 : 1);
                            saleChance.setChId(Long.parseLong(chidStr));
                    }else {
                            if (cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setChId(Long.parseLong(cell.getStringCellValue()));//ID
                            }
                        }
                    }else if (c == 1){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String chanceTheme = String.valueOf(cell.getNumericCellValue());
                            chanceTheme = chanceTheme.substring(0,chanceTheme.length() - 2 > 0 ? chanceTheme.length() - 2 : 1);

                        }else {
                            saleChance.setChanceTheme(cell.getStringCellValue());//机会主题
                        }
                    }else if (c == 2){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String clientIdStr = String.valueOf(cell.getNumericCellValue());
                            clientIdStr = clientIdStr.substring(0,clientIdStr.length() - 2 > 0 ? clientIdStr.length() - 2 : 1);

                            saleChance.setClientId(new BigDecimal(clientIdStr));
                        }else {
                            if (cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setClientId(new BigDecimal(cell.getStringCellValue()));//客户
                            }
                        }
                    }else if(c == 3){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String contactIdStr = String.valueOf(cell.getNumericCellValue());
                            contactIdStr = contactIdStr.substring(0,contactIdStr.length() - 2 > 0 ? contactIdStr.length() - 2 : 1);

                            saleChance.setContactId(new BigDecimal(contactIdStr));
                        }else {
                            if(cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setContactId(new BigDecimal(cell.getStringCellValue()));//客户联系人
                            }
                        }
                    }else if(c == 4){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String type = String.valueOf(cell.getNumericCellValue());
                            type = type.substring(0,type.length() - 2 > 0 ? type.length() - 2 : 1);
                        }else {
                            saleChance.setType(cell.getStringCellValue());//类型
                        }
                    }else if (c == 5){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String userIdStr = String.valueOf(cell.getNumericCellValue());
                            userIdStr = userIdStr.substring(0,userIdStr.length() - 2 > 0 ? userIdStr.length() - 2 : 1);

                            saleChance.setUserId(new BigDecimal(userIdStr));
                        }else {
                            if(cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setUserId(new BigDecimal(cell.getStringCellValue()));//提供人
                            }
                        }
                    }else  if (c == 6){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String expectDateStr = String.valueOf(cell.getNumericCellValue());
                            expectDateStr = expectDateStr.substring(0,expectDateStr.length() - 2 > 0 ? expectDateStr.length() - 2 : 1);
                            Date expectDate = fmt.parse(expectDateStr);
                            saleChance.setExpectDate(expectDate);
                        }else {
                            if (cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setExpectDate(fmt.parse(cell.getStringCellValue()));//预计签单日期
                            }
                        }
                    }else if (c == 7){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String expectMoneystr = String.valueOf(cell.getNumericCellValue());
                            expectMoneystr = expectMoneystr.substring(0,expectMoneystr.length() - 2 > 0 ? expectMoneystr.length() - 2 : 1 );

                            saleChance.setExpectMoney(new BigDecimal(expectMoneystr));

                        }else {
                            if(cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setExpectMoney(new BigDecimal(cell.getStringCellValue()));//预计金额
                            }
                        }
                    }else if(c == 8){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String possibilityStr = String.valueOf(cell.getNumericCellValue());
                            possibilityStr = possibilityStr.substring(0,possibilityStr.length() - 2 > 0 ? possibilityStr.length() - 2 : 1);

                        }else {
                            saleChance.setPossibility(cell.getStringCellValue());//可能性
                        }
                    }else if (c == 9){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String stageStr = String.valueOf(cell.getNumericCellValue());
                            stageStr = stageStr.substring(0,stageStr.length() - 2 > 0 ? stageStr.length() - 2 : 1);
                        }else {
                            saleChance.setStage(cell.getStringCellValue());//阶段
                        }
                    }else if(c == 10){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String stateStr = String.valueOf(cell.getNumericCellValue());
                            stateStr = stateStr.substring(0,stateStr.length() - 2 > 0 ? stateStr.length() - 2 : 1);
                        }else {
                            saleChance.setState(cell.getStringCellValue());//状态
                        }
                    }else if(c == 11){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String updateDateStr = String.valueOf(cell.getNumericCellValue());
                            updateDateStr = updateDateStr.substring(0,updateDateStr.length() - 2 > 0 ? updateDateStr.length() - 2 : 1);
                            Date updateDate = fmt.parse(updateDateStr);
                            saleChance.setUpdateDate(updateDate);
                        }else {
                            if(cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setUpdateDate(fmt.parse(cell.getStringCellValue()));//更新日期
                            }
                        }
                    }else if(c == 12){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String stageRemainStr = String.valueOf(cell.getNumericCellValue());
                            stageRemainStr = stageRemainStr.substring(0,stageRemainStr.length() - 2 > 0 ? stageRemainStr.length() - 2 : 1);

                            saleChance.setStageRemain(new BigDecimal(stageRemainStr));
                        }else {
                           if(cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                               saleChance.setStageRemain(new BigDecimal(cell.getStringCellValue()));//阶段停留
                           }
                        }
                    }else  if(c == 13){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String findTimeStr = String.valueOf(cell.getNumericCellValue());
                            findTimeStr = findTimeStr.substring(0,findTimeStr.length() - 2 > 0 ? findTimeStr.length() - 2 : 1);
                            Date findTime = fmt.parse(findTimeStr);
                            saleChance.setFindTime(findTime);
                        }else {
                            if(cell.getStringCellValue()!=null && !cell.getStringCellValue().isEmpty()){
                                saleChance.setFindTime(fmt.parse(cell.getStringCellValue()));//发现时间
                            }
                        }
                    }else if(c == 14){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String asteriskStr = String.valueOf(cell.getNumericCellValue());
                            asteriskStr = asteriskStr.substring(0,asteriskStr.length() - 2 > 0 ? asteriskStr.length() - 2 : 1);

                        }else {
                            saleChance.setAsterisk(cell.getStringCellValue());//星标
                        }
                    }else if(c == 15){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String intentRpoductStr = String.valueOf(cell.getNumericCellValue());
                            intentRpoductStr = intentRpoductStr.substring(0,intentRpoductStr.length() - 2 > 0 ? intentRpoductStr.length() - 2 : 1);

                        }else {
                            saleChance.setIntentRpoduct(cell.getStringCellValue());//意向产品
                        }
                    }else if(c == 16){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String succedaneumStr = String.valueOf(cell.getNumericCellValue());
                            succedaneumStr = succedaneumStr.substring(0,succedaneumStr.length() - 2 > 0 ? succedaneumStr.length() - 2 : 1);

                        }else {
                            saleChance.setSuccedaneum(cell.getStringCellValue());//替代品威胁
                        }
                    }else  if (c == 17){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String outlineNeedStr = String.valueOf(cell.getNumericCellValue());
                            outlineNeedStr = outlineNeedStr.substring(0,outlineNeedStr.length() - 2 > 0 ? outlineNeedStr.length() - 2 : 1);
                        }else {
                            saleChance.setOutlineNeed(cell.getStringCellValue());//需求概要
                        }

                    }else if(c == 18){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String remarks = String.valueOf(cell.getNumericCellValue());
                            remarks = remarks.substring(0,remarks.length() - 2 > 0 ? remarks.length() - 2 : 1);
                        }else {
                            saleChance.setRemarks(cell.getStringCellValue());//备注
                        }
                    }else if (c == 19){
                        if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String saleChanceIsDel = String.valueOf(cell.getNumericCellValue());

                        }else {
                            saleChance.setSaleChanceIsDel(cell.getStringCellValue());//是否删除
                        }
                    }

                }
            }
            saleChanceList.add(saleChance);
        }


        return saleChanceList;
    }
}
